using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using JQueryandMVC.Extensions;

namespace JQueryandMVC.Controllers
{
    public class CalendarController : Controller
    {
        //
        // GET: /Calendar/

        public ActionResult Index()
        {
            return View();
        }

        //http://www.redredred.com.au/projects/jquery-week-calendar/
        public ActionResult WeekCalendar()
        {
            return View();
        }
        public ActionResult MonthCalendar()
        {
            return View();
        }
        public ActionResult MonthEvents(int year, int month)
        {
            if (year == 0)
                year = DateTime.Now.Year;

            if (month == 0)
                month = DateTime.Now.Month;
            var Event = new {
                         EventID = 101, StartDateTime=  "new Date(2009, 9, 2)",
                         EndDateTime = "new Date(2009, 9, 3)", 
                         Title = "10:00 pm - EventTitle6",
                         URL = "#", Description = "This is a sample event description" };
            var Event2 = new
            {
                EventID = 102,
                StartDateTime = "new Date(2009, 9, 29)",
                EndDateTime = "new Date(2009, 9, 30)",
                Title = "10:00 pm - EventTitle6",
                URL = "#",
                Description = "This is a sample event description"
            };

            var events = new [] {Event,Event2};
            //JsonResult result = new JsonResult();
            //result.Data = events;
            return Json(new
                {
                    Results = 2,
                    Events = events
                });
        }
        public ActionResult WeekEvents(int year, int month,int day)
        {

            if (year == 0)
                year = DateTime.Now.Year;

            if (month == 0)
                month = DateTime.Now.Month;
            if (day == 0)
                day = DateTime.Now.Day;

            var Event = new
            {   //1997-07-16T19:20+01:00
                id = Guid.NewGuid(), 
                //start =   year.ToString() + "-0" + month.ToString() + "-" + day.ToString() + "T12:00:00+10:00",
                start = (new DateTime(year,month,day,12,0,0)).ConvertToJSDT(),
                end = new DateTime(year,month,day,14,0,0).ConvertToJSDT(),
                title = "test1",body = "test 1 body" };
            var Event2 = new
            {
                id = Guid.NewGuid(), 
                start = new DateTime(year,month,day +1,12,0,0).ConvertToJSDT(),
                end = new DateTime(year, month, day + 1, 14, 30, 0).ConvertToJSDT(),
                title = "test2" , body= "test2 body"
            };

            var events = new[] { Event, Event2 };
            //JsonResult result = new JsonResult();
            //result.Data = events;
            return Json(new
            {
                Results = 2,
                Events = events
            });
        }
        //http://delete.me.uk/2005/03/iso8601.html
        public ActionResult SavingEvent(Guid? id,string start,string end,string title,string body){
            DateTime startDt = convertDateTime(start);
            DateTime endDt = convertDateTime(end);
            var SavedEvent = new { id = Guid.NewGuid(), start = startDt.ConvertToJSDT(), end = endDt.ConvertToJSDT(), title = title, body = body };
            return Json( new {success = "OK", Event = SavedEvent });
        }

        private DateTime convertDateTime(string DateTimeStr)
        {
            string[] DtArray = DateTimeStr.Split('-');
            DateTime convertedDt = new DateTime(
                         int.Parse(DtArray[0]), int.Parse(DtArray[1]), int.Parse(DtArray[2]),
                         int.Parse(DtArray[3]), int.Parse(DtArray[4]), int.Parse(DtArray[5]));
            return convertedDt;
        }
    }
}

    //function zapiszEvent(calEvent,akcja) {
    //    $.post(akcja, {
    //        id: calEvent.id,
    //        club_id: klub_id,
    //        start: calEvent.start ,
    //        end: calEvent.end,
    //        nazwa: calEvent.title,
    //        opis: calEvent.body                                 
    //    }, function(data){
    //    if(data != "OK") {
    //      alert("Komunikat bledu: " + data);
    //    }
    //});

    //                    $.post("/zf/ajax/deleteevent", {
    //                        id: calEvent.id,
    //                        club_id: klub_id                                 
    //                    }, function(data){
    //                    if(data != "OK") {
    //                      alert("Komunikat bledu: " + data);
    //                    }


    //var rows = (Person.GetABunchOfPeople()
    //    .Select(c => new
    //                     {
    //                         id = c.ID,
    //                         cell = new[]
    //                                    {
    //                                        c.ID.ToString(),
    //                                        c.Name,
    //                                        c.Birthday.ToShortDateString()
    //                                    }
    //                     })).ToArray();

    //return new JsonResult
    //           {
    //               Data = new
    //                          {
    //                              page = 1,
    //                              records = rows.Length,
    //                              rows,
    //                              total = 1
    //                          }
    //           };

/* accepted values for the format [1-6]:
  1 Year:
    YYYY (eg 1997)
  2 Year and month:
    YYYY-MM (eg 1997-07)
  3 Complete date:
    YYYY-MM-DD (eg 1997-07-16)
  4 Complete date plus hours and minutes:
    YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
  5 Complete date plus hours, minutes and seconds:
    YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
  6 Complete date plus hours, minutes, seconds and a decimal
    fraction of a second
    YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
 */
